package com.city.eduservice.controller;


import com.city.commonutils.Result;
import com.city.eduservice.entity.EduChapter;
import com.city.eduservice.entity.vo.chapthe.ChapterVo;
import com.city.eduservice.service.EduChapterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author city
 * @since 2020-07-01
 */
@RestController
@RequestMapping("/eduservice/chapter")
@CrossOrigin
public class EduChapterController {

    @Autowired
    private EduChapterService eduChapterService;

    /**
     * 根据课程id获取章节和小节
     * @param courseId
     * @return
     */
    @GetMapping("getChapterVideo/{courseId}")
    public Result getChapterVideo(@PathVariable String courseId){
        List<ChapterVo> list = eduChapterService.getChapterVideoByCourseId(courseId);
        return Result.ok().data("allChapterVideo",list);
    }

    /**
     * 前端操作单独添加章节，在单独设置小节
     */
    @PostMapping("addChapter")
    public Result addChapter(@RequestBody EduChapter eduChapter){
        eduChapterService.save(eduChapter);
        return Result.ok();
    }

    /**
     * 修改章节前获取章节
     */
    @GetMapping("getChapterInfo/{chapterId}")
    public Result getChapterInfo(@PathVariable String chapterId){
        EduChapter byId = eduChapterService.getById(chapterId);
        return Result.ok().data("chapter",byId);
    }

    @PostMapping("updateChapter")
    public Result updateChapter(@RequestBody EduChapter eduChapter){
        eduChapterService.updateById(eduChapter);
        return Result.ok();
    }

    @DeleteMapping("{chapterId}")
    public Result deleteChapter(@PathVariable String chapterId){
        boolean b = eduChapterService.deleteChapter(chapterId);
        if (b) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }

}

